﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('GMM_GiftMaterialRuleMain.Add')" icon="plus" type="primary" @click="handleAdd()"
      >新建
      </a-button
      >
      <a-button
        v-if="hasPerm('GMM_GiftMaterialRuleMain.Delete')"
        :disabled="!hasSelected()"
        :loading="loading"
        icon="minus"
        type="primary"
        @click="handleDelete(selectedRowKeys)"
      >删除
      </a-button
      >
      <a-button icon="redo" type="primary" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select v-model="queryParam.condition" allowClear>
                <a-select-option key="RuleCode">规则编码</a-select-option>
                <a-select-option key="RuleName">规则名称</a-select-option>
                <a-select-option key="GiftType">返料类型</a-select-option>

                <!-- <a-select-option key="State">状态</a-select-option> -->
                <!-- <a-select-option key="Remark">备注</a-select-option>
                <a-select-option key="GiftDeliveryBasics">返料发货基础</a-select-option>
                 -->
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" @keydown.enter="getDataList"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
            >查询
            </a-button>
            <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :bordered="true"
      :columns="columns"
      :dataSource="data"
      :loading="loading"
      :pagination="pagination"
      :rowKey="(row, index) => row.Id"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :scroll="{ y: 530 }"
      size="small"
      @change="handleTableChange"
    >
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="StartDate" slot-scope="StartDate">{{ StartDate.substring(0, 10) }}</span>
      <span slot="EndDate" slot-scope="EndDate">{{ EndDate.substring(0, 10) }}</span>
      <span slot="GiftType" slot-scope="text">
        <a-tag :color="text == '销售返料' ? 'green' : text == '销售返利' ? 'blue' : ''">
          {{ text }}
        </a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('GMM_GiftMaterialRuleMain.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider
            v-if="hasPerm('GMM_GiftMaterialRuleMain.Edit') && hasPerm('GMM_GiftMaterialRuleMain.Delete')"
            type="vertical"
          />
          <a v-if="hasPerm('GMM_GiftMaterialRuleMain.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import moment from 'moment'

const columns = [
  // { title: '更新时间', dataIndex: 'UpdateTime', width: '10%' },
  { title: '规则编码', dataIndex: 'RuleCode', align: 'center', width: '10%' },
  { title: '规则名称', dataIndex: 'RuleName' },
  {
    title: '开始效期',
    dataIndex: 'StartDate',
    align: 'center',
    customRender: value => {
      if (value) return value.substring(0, 10)
    },
    width: '10%'
  },
  {
    title: '结束效期',
    dataIndex: 'EndDate',
    align: 'center',
    customRender: value => {
      if (value) return value.substring(0, 10)
    },
    width: '10%'
  },

  {
    title: '返料类型',
    align: 'center',
    dataIndex: 'GiftType',
    scopedSlots: { customRender: 'GiftType' },
    width: '10%'
  },

  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '10%' }
]

export default {
  components: {
    EditForm
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'StartDate', order: 'desc' },
      loading: false,
      columns,
      queryParam: { condition: 'RuleName' },
      selectedRowKeys: []
    }
  },
  methods: {
    repossess () {
      this.queryParam = { condition: 'RuleName' }
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/GiftManagement/GMM_GiftMaterialRuleMain/GetDataListByConditions', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: [this.queryParam],
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建返料规则')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑返料规则')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/GiftManagement/GMM_GiftMaterialRuleMain/DeleteDataOfAll', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
